﻿@{
	Layout = null;
}
<!DOCTYPE html>
<html>
<head>
	<meta name="viewport" content="width=device-width" />
	<title>Dos.ORM初级玩法demo之mvc简单三层</title>
	<link href="~/Content/Bootstrap/bootstrap.min.css" rel="stylesheet" />
	<script src="~/Scripts/jquery-1.11.1.min.js"></script>
	<script src="~/Scripts/Common.js"></script>
	<script src="~/Scripts/avalon.min.js"></script>
	<script src="~/Content/Bootstrap/bootstrap.min.js"></script>
</head>
<body ms-controller="avBody" style="padding: 10px;">
	<h2>Dos.ORM初级玩法demo之mvc简单三层</h2>
	<p>
		姓名：<input type="text" ms-duplex="SearchName" />
		身份证号：<input type="text" ms-duplex="SearchIDNumber" />
		手机号：<input type="text" ms-duplex="SearchMobilePhone" />
		<input value="查询" type="button" class="btn btn-primary btn-xs" ms-click="GetUser(1)" />
	</p>
	<p>
		<table class="table table-bordered table-hover">
			<tr>
				<td>序号</td>
				<td>姓名</td>
				<td>身份证号</td>
				<td>手机号</td>
				<td>添加时间</td>
				<td>操作</td>
			</tr>
			<tr ms-repeat="Users">
				<td>{{$index + 1}}</td>
				<td>
					<input type="text" ms-value="el.Name" ms-attr-id="'Name_'+el.Id" />
				</td>
				<td>
					<input type="text" ms-value="el.IDNumber" ms-attr-id="'IDNumber_'+el.Id" />
				</td>
				<td>
					<input type="text" ms-value="el.MobilePhone" ms-attr-id="'MobilePhone_'+el.Id" />
				</td>
				<td>
					{{ShiftDate(el.CreateTime).Format('yyyy-MM-dd HH:mm:ss')}}
				</td>
				<td>
					<input value="修改" type="button" class="btn btn-primary btn-xs" ms-click="UptUser(el)" />
					<input value="删除" type="button" class="btn btn-primary btn-xs" ms-click="DelUser(el)" />
				</td>
			</tr>
		</table>
	</p>
	<div style="margin: 10px; text-align: right;">
		<div ms-widget="pager,pg"></div>
	</div>
	<p>
		姓名：<input type="text" ms-duplex="Name" />
		身份证号：<input type="text" ms-duplex="IDNumber" />
		手机号：<input type="text" ms-duplex="MobilePhone" />
		<input value="新增" type="button" class="btn btn-primary btn-xs" ms-click="AddUser" />
	</p>
<p>
	<a href="http://www.itdos.com/CSharp/20150803/0219373.html" target="_blank" style="font-size: 18px; font-weight: bold;">http://www.itdos.com/CSharp/20150803/0219373.html</a>
</p>
	<p style="color: red;">注意：由于demo默认是使用SQLite数据库，初级demo与中级demo不能同时运行连接SQLite数据库。</p>
	<script type="text/javascript">
		var avBody;
		require(["/avalonUI/avalon.pager"], function () {
			avBody = avalon.define("avBody", function (vm) {
				vm.SearchName = "";
				vm.SearchIDNumber = "";
				vm.SearchMobilePhone = "";
				vm.Name = "";
				vm.IDNumber = "";
				vm.MobilePhone = "";
				vm.Users = [];
				vm.PageSize = 5;
				vm.PageIndex = 1;
				vm.pager = {
					onInit: function () {
						avBody.GetUser(1);
					},
					currentPage: 1,
					perPages: 10,
					totalItems: 0,
					showJumper: true,
					onJump: function (e, page) {
						avBody.PageIndex = page.currentPage - 1;
						avBody.GetUser(page.currentPage);
					}
				}
				vm.$skipArray = ["pager"];
				//获取数据
				vm.GetUser = function (pageIndex) {
					$.post("Home/GetUser", {
						SearchName: avBody.SearchName,
						SearchIDNumber: avBody.SearchIDNumber,
						SearchMobilePhone: avBody.SearchMobilePhone,
						_PageIndex: pageIndex,
						_PageSize: avBody.PageSize
					}, function (result) {
						if (result.IsSuccess) {
							avBody.Users = result.Data;
							var widget = avalon.vmodels.pg;
							if (widget && widget.totalItems !== result.DataCount) {
								widget.totalItems = result.DataCount;
							}
						} else {
							alert("获取数据失败！异常信息：" + result.Message);
						}
					});
				};
				//新增数据
				vm.AddUser = function () {
					$.post("Home/AddUser", {
						Name: avBody.Name,
						IDNumber: avBody.IDNumber,
						MobilePhone: avBody.MobilePhone
					}, function (result) {
						if (result.IsSuccess) {
							avBody.GetUser(1);
							alert("新增成功！");
						} else {
							alert("新增失败！异常信息：" + result.Message);
						}
					});
				};
				//删除数据
				vm.DelUser = function (el) {
					if (confirm("确定删除【" + el.Name + "】？")) {
						$.post("Home/DelUser", {
							Id: el.Id
						}, function (result) {
							if (result.IsSuccess) {
								avBody.GetUser(1);
								alert("删除成功！");
							} else {
								alert("删除失败！异常信息：" + result.Message);
							}
						});
					}
				};
				//修改数据
				vm.UptUser = function (el) {
					$.post("Home/UptUser", {
						Id: el.Id,
						Name: $("#Name_" + el.Id).val(),
						IDNumber: $("#IDNumber_" + el.Id).val(),
						MobilePhone: $("#MobilePhone_" + el.Id).val()
					}, function (result) {
						if (result.IsSuccess) {
							avBody.GetUser(1);
							alert("修改成功！");
						} else {
							alert("修改失败！异常信息：" + result.Message);
						}
					});
				};
			});
			avalon.scan();
		});
	</script>
</body>
</html>
